package com.isoftstone.merchant.controller.mproject;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.mproject.MProject;
import com.isoftstone.merchant.entity.mprojecttype.MProjectType;
import com.isoftstone.merchant.service.mproject.MProjectService;
import com.isoftstone.merchant.service.mprojecttype.MProjectTypeService;
import com.isoftstone.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by as on 2017/12/12.
 * 收费项目设置
 */
@Controller
public class MProjectController extends BaseController{
    @Autowired
    MProjectService mProjectService;
    @Autowired
    MProjectTypeService mProjectTypeService;
    /*
   * 到达项目类型设置页面
   * */
    @RequiresPermissions("mms-menu.003.002")
    @RequestMapping("merchant/mProject/getProjectList")
    public ModelAndView toMProjectType(){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/charge_management/charge_item_set");
        return md;
    }
    @RequestMapping("merchant/mProject/getProjectList1")
    @ResponseBody
    public Object mProjectTypeList(Page page){
        PageData pd = this.getPageData();
        Tidings<List<MProject>> tidings = new Tidings<List<MProject>>();
        String keywords=pd.getString("searchContent");
        String corpId=this.getUser().getCorpId();
        pd.put("corpId",corpId);
        try {
            if(StringUtils.isNotBlank(keywords)){
                keywords = URLDecoder.decode(keywords, "UTF-8");
                pd.put("keywords",keywords);
            }
            page.setPd(pd);
            List<MProject> mProjectTypeList = mProjectService.findprojectlistPage(page);
            tidings.setT(mProjectTypeList).setPage(page);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings ;
    }
    @RequestMapping("merchant/mProject/saveOrUpdateMProject")
    @ResponseBody
    public Object addMProjectType(){
        PageData pd = this.getPageData();
        MProject mProject=new MProject();
        Tidings<List<MProject>> tidings = new Tidings<List<MProject>>();
        String corpId=this.getUser().getCorpId();
        String userId=this.getUser().getUserid();
        String ProjectId=pd.getString("projectId");
        if(StringUtils.isNotBlank(ProjectId)){
            mProject.setProjectId(Long.parseLong(ProjectId));
        }
        String statrDate=pd.getString("startDate");
        String endDate=pd.getString("endDate");
        if(StringUtils.isNotBlank(statrDate)&&StringUtils.isNotBlank(endDate)){
            statrDate = statrDate.replace("GMT", "").replaceAll("\\(.*\\)", "");
            endDate=endDate.replace("GMT", "").replaceAll("\\(.*\\)", "");
        }else{
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("请填写日期");
            return tidings;
        }
        String projectDesc=pd.getString("projectDesc");
        String projectName=pd.getString("projectName");
        SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
        if(StringUtils.isNotBlank(projectName)){
            mProject.setProjectName(projectName);
        }
        if(StringUtils.isNotBlank(projectDesc)){
            mProject.setProjectDesc(projectDesc);
        }
        String projectTyprId=pd.getString("projectTypeId");
        if(StringUtils.isNotBlank(projectTyprId)){
            mProject.setProjectTypeId(Long.parseLong(projectTyprId) );
        }
        if(StringUtils.isNotBlank(statrDate)){
            Integer index=statrDate.indexOf("-");
            try {
                if(index==-1){
                Date date = sf1.parse(statrDate);
                SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
                Date  statrDate1= DateUtil.fomatDate(sf2.format(date));
                mProject.setStartDate(statrDate1);
                } else{
                    Date  statrDate1= DateUtil.fomatDate(statrDate);
                    mProject.setStartDate(statrDate1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(StringUtils.isNotBlank(endDate)){
            Integer index=endDate.indexOf("-");
            try {
                if(index==-1) {
                    Date date = sf1.parse(endDate);
                    SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
                    Date endate = DateUtil.fomatDate(sf2.format(date));
                    mProject.setEndDate(endate);
                }else{
                    Date endate = DateUtil.fomatDate(endDate);
                    mProject.setEndDate(endate);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("cropId",corpId);
        List<MProject>  MP=null;
        try {
            if(StringUtils.isNotBlank(mProject.getProjectName())){
                map.put("name",mProject.getProjectName());
                if(mProject.getProjectId()!=null){
                    MP=mProjectService.checkName(map);
                    if(MP.size()==1){
                        if(MP.get(0).getProjectId().equals(mProject.getProjectId())){
                            mProject.setUpdateDate(new Date());
                            mProject.setUpdator(userId);
                            mProjectService.updateMProjectType(mProject);
                            tidings.setStatus(Const.SUCCESS);
                            tidings.setMsg("修改成功");
                            return tidings;
                        }else{
                            tidings.setStatus(Const.SUCCESS);
                            tidings.setMsg("项目名称已经存在");
                            return tidings;
                        }
                    }else if(MP.size()==0||MP==null){
                        mProject.setUpdateDate(new Date());
                        mProject.setUpdator(userId);
                        mProjectService.updateMProjectType(mProject);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("修改成功");
                        return tidings;
                    }else{
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("项目名称已经存在");
                        return tidings;
                    }
                }else{
                    MP=mProjectService.checkName(map);
                    if(MP.size()>0){
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("项目名称已经存在");
                        return tidings;
                    }else{
                        String id = UuidUtil.getNumFormatUUID();
                        mProject.setProjectId(Long.parseLong(id));
                        mProject.setStatus("1");//1启用0禁用
                        mProject.setCorpId(corpId);
                        mProject.setCreator(userId);
                        mProject.setCreateDate(new Date());
                        mProjectService.saveMProjectType(mProject);
                        tidings.setStatus(Const.SUCCESS);
                        tidings.setMsg("添加成功");
                        return tidings;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("操作失败");
        }
        return tidings;
    }
    @RequestMapping("merchant/mProject/deleteMProject")
    @ResponseBody
    public Object deleteMProjectType(){
        PageData pd = this.getPageData();
        Tidings<String > tidings = new Tidings<String>();
        String mProjectId=pd.getString("projectId");
        Map<String,Object>map=new HashMap<String ,Object>();
        String payStatus="04";
        map.put("payStatus",payStatus);
        map.put("mProjectId",Long.parseLong(mProjectId));
        try {
            Integer count=mProjectService.getCountFromMProject(map);
            if(count!=null&&count>0){
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("该收费项目存在待缴费不能删除");
            }else{
                mProjectService.deleteMProjectType(Long.parseLong(mProjectId));
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除失败");
        }
        return tidings;
    }
    @RequestMapping("merchant/mProject/getProjectType")
    @ResponseBody
    public Object getProjectType(){
        Tidings< List<MProjectType> > tidings = new Tidings< List<MProjectType>>();
        String corpId=this.getUser().getCorpId();
        String status="1";
        Map<String,Object>map =new HashMap<String ,Object>();
        map.put("corpId",corpId);
        map.put("status",status);
        List<MProjectType>typeList= null;
        try {
            typeList = mProjectTypeService.getTypeList(map);
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(typeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tidings;
    }
}
